package com.wh.wisdomsite.xbox.server;

import com.wh.wisdomsite.xbox.ba.service.IAppMethodService;
import com.wh.wisdomsite.xbox.ba.service.IAppSecretService;
import com.wh.wisdomsite.xbox.ba.service.IServiceMethodService;
import com.wh.wisdomsite.xbox.core.ServiceDispatcher;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;

/**
 * 功能描述：控制台输出
 * @Package com.wh.wisdomsite.xbox.server
 * @author 谢泽鹏
 * @date 2014年10月8日 下午4:02:14
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2014
 * @version V1.0
 */
@Controller
public class ConsoleController {

    private static final Log logger = LogFactory.getLog(ConsoleController.class);

    @Resource(name = "serviceDispatcher")
    private ServiceDispatcher serviceDispatcher;

    @Autowired
    private IServiceMethodService iServiceMethodService;

    @Autowired
    private IAppSecretService iAppSecretService;

    @Autowired
    private IAppMethodService iAppMethodService;

    /**
     * 页面跳转(数据信息)
     * @param model
     * @param request
     * @return
     */
    @RequiresPermissions("rules:dataServices_page")
    @RequestMapping(value = {"", "index.do"}, method = RequestMethod.GET)
    public String index_ui(Model model,HttpServletRequest request) {
        //获取线程池的状态
        Map<String, Object> threadStatus = serviceDispatcher.getJobDispathcher().getCurrentThreadStatus();
        Iterator it = threadStatus.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry e = (Map.Entry) it.next();
            model.addAttribute(e.getKey().toString(), e.getValue());
        }
        return "/dataServices/dataInfo/xboxConfig";
    }

    /**
     * 重置线程池大小
     * @param maxSize
     * @param coreSize
     * @param model
     * @return
     */
    @RequestMapping(value = "/resetThreadPoolSize.do",method = RequestMethod.GET)
    public String setThreadPoolSize(@RequestParam(value = "maxSize") Integer maxSize, @RequestParam(value = "coreSize") Integer coreSize, Model model) {
        try {
            serviceDispatcher.getJobDispathcher().getJobThreadPoolExecutor().setCorePoolSize(coreSize);
            serviceDispatcher.getJobDispathcher().getJobThreadPoolExecutor().setMaximumPoolSize(maxSize);
            model.addAttribute("resetThreadPoolSize", 1);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("resetThreadPoolSize", 0);
        }
        return "/dataServices/dataInfo/xboxConfig";
    }

    /**
     * 对bean的某个域赋值默认值 string = "" int = 0
     * @param field
     * @param target
     * @param value
     */
    private void setFieldValueToDefault(Field field, Object target, Object value) {
        try {
            if (value != null) {
                field.setAccessible(true);
                field.set(target, value);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }


}
